//package com.finalterm.gateway.filter;
//
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.cloud.gateway.filter.GatewayFilterChain;
//import org.springframework.cloud.gateway.filter.GlobalFilter;
//import org.springframework.core.Ordered;
//import org.springframework.http.server.reactive.ServerHttpRequest;
//import org.springframework.stereotype.Component;
//import org.springframework.web.server.ServerWebExchange;
//import reactor.core.publisher.Mono;
//
///**
// * 日志记录全局过滤器
// */
//@Slf4j
//@Component
//public class LoggingGlobalFilter implements GlobalFilter, Ordered {
//
//    @Override
//    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//        ServerHttpRequest request = exchange.getRequest();
//
//        long startTime = System.currentTimeMillis();
//        String requestId = java.util.UUID.randomUUID().toString();
//
//        log.info("请求开始 - RequestId: {}, Method: {}, URI: {}, RemoteAddr: {}",
//                requestId,
//                request.getMethod(),
//                request.getURI(),
//                getClientIp(request));
//
//        return chain.filter(exchange).then(
//            Mono.fromRunnable(() -> {
//                long endTime = System.currentTimeMillis();
//                log.info("请求结束 - RequestId: {}, 耗时: {}ms, 状态码: {}",
//                        requestId,
//                        endTime - startTime,
//                        exchange.getResponse().getStatusCode());
//            })
//        );
//    }
//
//    /**
//     * 获取客户端IP
//     */
//    private String getClientIp(ServerHttpRequest request) {
//        String xForwardedFor = request.getHeaders().getFirst("X-Forwarded-For");
//        if (xForwardedFor != null && !xForwardedFor.isEmpty()) {
//            return xForwardedFor.split(",")[0].trim();
//        }
//
//        String xRealIp = request.getHeaders().getFirst("X-Real-IP");
//        if (xRealIp != null && !xRealIp.isEmpty()) {
//            return xRealIp;
//        }
//
//        return request.getRemoteAddress() != null ?
//                request.getRemoteAddress().getAddress().getHostAddress() : "unknown";
//    }
//
//    @Override
//    public int getOrder() {
//        return -200;
//    }
//}
